%if ( $err_msg ) {
<h2>Error</h2>
<p>
  no samples selected
</p>
<pre>
  Tag list
  ========
  <% Dumper(\@tag_list) %>


  Args
  --------
  <% Dumper(\%ARGS) %>
</pre>
% } else {  
<h2>Sample labels</h2>
<table cellspacing="0" cellpadding="2">
%   foreach ( @list ) {
%     (undef, my $batch, my $sample) = split ":";
%     my $sample_file = "$TRFLP::data_path/$batch/$sample";
  <tr>
    <td><% "$batch/$sample" %></td>
    <td>-></td>
    <td><% $Rname{$sample_file} %></td>
  </tr>
%   }
</table>
<h2>Binned peaks</h2>
<table border cellspacing="0">
%   foreach ( @matr ) {
%     my ($label, @data) = split "\t";
%     $label =~ s/\"//g;
%     $label = join("/", (split "/", $label)[-2,-1]) unless $label eq "length";
   <tr>
     <td><% $label %></td>
%     foreach my $value ( @data ) {
%       if ( $label eq "length" ) {
     <td><% sprintf("%6.2f", $value) %></td>
%       } else {
     <td><% sprintf("%6.2g", $value) %></td>
%       }
%     }
%     if ( $label eq "length" ) {
     <td><b>Total</b></td>
%     } else {
%       my $sum = 0;
%       foreach (@data) {$sum += $_};
     <td><b><% sprintf("%4.2g", $sum) %></b></td>
%     }
   </tr>
%   }
</table>

<h2>Graphical view of the binned peaks</h2>
<img src="plot_cluster.html?<% gmtime %>">

<h2>Bray-Curtis distance matrix for Mega</h2>

<a href="matrix.meg">Download</a>
<pre>
<% `$TRFLP::/bin/braycurtis < $TRFLP::tmpdir/ClusBinMatrix.txt` %>
</pre>
% }

<%init>
my $err_msg = undef;
my @tag_list;
my %Rname;
my %R2Real;
my @matr;
my @list = grep !/threshold|error/, keys %ARGS;
if ( @list ) {
  my $nsamp = 0;
  my $prefix = 'sample';
  my $color = 'B';
  my @filtered_list;

  foreach ( @list ) {
    (undef, my $batch, my $sample) = split ":";
    my $sample_file = "$TRFLP::data_path/$batch/$sample";
    my @peaks = `cat $sample_file.abi`;
    next unless scalar @peaks >= 1;
    $nsamp++;
    push @filtered_list, $sample_file;
    $Rname{$sample_file} = "$prefix$nsamp";
    $R2Real{"$prefix$nsamp"} = $sample_file;
  }
  my $out_file = "$TRFLP::tmpdir/cluster.r";
  open OUTFILE, ">$out_file";

  print OUTFILE "\n\n";
  foreach my $sample ( @filtered_list ) {
    print OUTFILE <<END
$Rname{$sample} <- read.table(file="$sample.abi", col.names = c("Color", "time", "length", "height", "area", "point"))
$Rname{$sample}.$color <- cbind($Rname{$sample}\[grep("^$color", as.character($Rname{$sample}\$Color), perl = T), 3], $Rname{$sample}\[grep("^$color", as.character($Rname{$sample}\$Color), perl = T), 5])
$Rname{$sample}.$color <- cbind($Rname{$sample}.$color, $Rname{$sample}.$color\[,2]/sum($Rname{$sample}.$color\[,2]))

END
;
  }
  my $colored_list = join ", ", map {"$_.$color"} @Rname{@filtered_list};
  my $colnames = join ", ", qq("length"), map {qq("$_")} @filtered_list;
  print OUTFILE qq(
$color.list <- list($colored_list)
$color.Data <- Gfiltering.ftn($color.list, $threshold)
$color.ClusBin <- GclustBin.ftn($color.Data[[1]], $error)
ClustBin <- rbind(B.ClusBin)
write.table(t($color.ClusBin), file="$TRFLP::tmpdir/ClusBinMatrix.txt", sep="\\t", row.names=c($colnames), col.names=F)
);

  close OUTFILE;

  my $cmd =  "cat $TRFLP::bin/FilteringandBinning.r $TRFLP::tmpdir/cluster.r | R --vanilla --slave >& $TRFLP::tmpdir/R.log";
  die "couldn't run command: $cmd; reason: $!" unless system($cmd) == 0;

  @matr = `cat $TRFLP::tmpdir/ClusBinMatrix.txt`;
}
else {
  $err_msg = "No samples selected"
}
</%init>

<%flags>
  inherit => 'syshandler'
</%flags>

<%args>
  $error
  $threshold
</%args>
